﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace UCS.XNA.Utils
{
    public class TextBoxMenuItem : MenuItem
    {
        #region Constants

        private const float TEXT_BOX_HEIGHT_PERCENTAGE = 0.7f;
        private const float TEXT_BOX_WIDTH_PERCENTAGE = 0.6f;
        private const float TEXT_WIDTH_PERCENTAGE = 0.3f;
        private const float LEFT_MARGIN_PERCENTAGE = 0.03f;
        private const float MIDDLE_MARGIN_PERCENTAGE = 0.04f;

        #endregion

        #region Attributes and Properties

        public override Rectangle Bounds
        {
            get { return base.Bounds; }
            set
            {
                base.Bounds = value;

                this.InitializeTextBoxBounds();
            }
        }

        public override bool Focused
        {
            get { return base.Focused; }
            set
            {
                base.Focused = value;

                this._textBox.Focused = base.Focused;
            }
        }

        public string TextBoxText
        {
            get { return this._textBox.Text; }
            set { this._textBox.Text = value; }
        }

        public bool ReadOnly
        {
            get { return this._textBox.ReadOnly; }
            set { this._textBox.ReadOnly = value; }
        }

        #endregion

        #region Private Members

        private TextBox _textBox;

        #endregion

        #region Constructors

        public TextBoxMenuItem(string text, SpriteFont spriteFont) : base(text, spriteFont)
        {
            this.InitializeTextBox();
        }

        #endregion

        #region Private Methods

        private void InitializeTextBox()
        {
            this._textBox = new TextBox(new Rectangle(), this.SpriteFont);

            this.InitializeTextBoxBounds();
        }

        private void InitializeTextBoxBounds()
        {
            Rectangle textBoxBounds = new Rectangle();
            textBoxBounds.X = this.Bounds.X + (int)(this.Bounds.Width * (LEFT_MARGIN_PERCENTAGE + TEXT_WIDTH_PERCENTAGE + MIDDLE_MARGIN_PERCENTAGE));
            textBoxBounds.Y = this.Bounds.Y + this.Bounds.Height / 2 - (int)(this.Bounds.Height * TEXT_BOX_HEIGHT_PERCENTAGE) / 2;
            textBoxBounds.Width = (int)(this.Bounds.Width * TEXT_BOX_WIDTH_PERCENTAGE);
            textBoxBounds.Height = (int)(this.Bounds.Height * TEXT_BOX_HEIGHT_PERCENTAGE);

            this._textBox.Bounds = textBoxBounds;
        }

        #endregion

        #region Overriden Methods

        protected override void InitializeTextPosition()
        {
            var textSize = this.SpriteFont.MeasureString(this.Text);
            int x = (int)(this.Bounds.X + (this.Bounds.Width * LEFT_MARGIN_PERCENTAGE));
            int y = (int)(this.Bounds.Y + (this.Bounds.Height - textSize.Y) / 2);
            this._textPosition = new Vector2(x, y);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            if (this._visible)
                this._textBox.Draw(gameTime);
        }

        public override void Update(GameTime gameTime, MouseState mouseState, KeyboardState keyboardState)
        {
            base.Update(gameTime, mouseState, keyboardState);

            this._textBox.Update(gameTime, keyboardState);
        }

        public override void Hide()
        {
            base.Hide();
            this._textBox.Hide();
        }

        public override void Show()
        {
            base.Show();
            this._textBox.Show();
        }

        #endregion
    }
}
